package com.jic.market.impl.allocation;

import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.market.common.enums.CostAllocationFlagEnum;
import com.jic.market.entity.AllocationModel;
import com.jic.market.entity.base.BaseActivity;
import com.jic.market.request.after.TellAddAllocationRequest;
import com.jic.market.service.allocation.PromotionAddAllocationStrategy;
import com.jic.market.service.base.BaseActivityService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author xuyongliang
 * @version V1.0
 * @className PromotionAddAllocationStrategyCoupon
 * @description 优惠券分摊策略
 * @date 2020/3/16
 */
@Slf4j
@Service("allocationCoupon")
public class PromotionAddAllocationStrategyCoupon implements PromotionAddAllocationStrategy {

    @Resource
    private BaseActivityService baseActivityService;
    @Override
    public AllocationModel collectionAllocation(TellAddAllocationRequest request) {
        log.info("--------进入优惠券分摊策略tellRequest--{}-----",request);
        String promotionId =request.getPromotionId();
        BaseActivity baseActivity = baseActivityService.queryActivityByPromotionId(promotionId);

        // NO CostAllocationFlagEnum.SHARE.getCode().equals(baseActivity.getCostAllocationFlag())
        AllocationModel model =new AllocationModel();
        BeanUtils.copyProperties(baseActivity, model);
        model.setPromotionId(baseActivity.getPromotionId());
        model.setCostAllocationFlag(baseActivity.getCostAllocationFlag());
        if (baseActivity.getCostAllocationFlag().equals("1")){
            if(!StringUtils.isEmpty(baseActivity.getParentAllocationScale())){
                model.setParentAllocationScale(Integer.valueOf(baseActivity.getParentAllocationScale()));
            }

            if(!StringUtils.isEmpty(baseActivity.getChildAllocationScale())){
                model.setChildAllocationScale(Integer.valueOf(baseActivity.getChildAllocationScale()));
            }

            if(!StringUtils.isEmpty(baseActivity.getMerchantAllocationScale())){
                model.setMerchantAllocationScale(Integer.valueOf(baseActivity.getMerchantAllocationScale()));
            }

            if(!StringUtils.isEmpty(baseActivity.getStoreAllocationScale())){
                model.setStoreAllocationScale(Integer.valueOf(baseActivity.getStoreAllocationScale()));
            }

            if(!StringUtils.isEmpty(baseActivity.getDeptId())){
                model.setDeptId(Long.valueOf(baseActivity.getDeptId()));
            }
        }else {
            //不分摊则判断当前活动组织等级设置默认值100
            if (baseActivity.getDeptType() == DeptTypeEnums.OPERATION_PLATFORM.getCode()){
                model.setParentAllocationScale(100);
            }else if (baseActivity.getDeptType() == DeptTypeEnums.CHILD_PLATFORM.getCode()){
                model.setChildAllocationScale(100);
            }else if (baseActivity.getDeptType() == DeptTypeEnums.MERCHANT_PLATFORM.getCode()){
                model.setMerchantAllocationScale(100);
            }else {
                model.setStoreAllocationScale(100);
            }
        }


        model.setDeptType(baseActivity.getDeptType());

        if(!StringUtils.isEmpty(baseActivity.getPromType())){
            model.setPromType(Integer.valueOf(baseActivity.getPromType()));
        }
        return model;
    }
}
